Move more input validation into BuildConfig::new()
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Sat, 14 Apr 2018 09:44:42 +0000 (11:44 +0200)
committerDirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 3 May 2018 19:54:48 +0000 (21:54 +0200)
src/cargo/core/compiler/mod.rs
src/cargo/ops/cargo_compile.rs

index 031321faf2d881de879498ac6e7510f0756e0841..498e594cd800daa9e0322d96a8fe9fff4c844aaa 100644 (file)
@@ -82,7 +82,18 @@ impl BuildConfig {
         rustc_info_cache: Option<PathBuf>,
         mode: CompileMode,
     ) -> CargoResult<BuildConfig> {
-        if let &Some(ref s) = requested_target {
+        let requested_target = match requested_target {
+            &Some(ref target) if target.ends_with(".json") => {
+                let path = Path::new(target)
+                    .canonicalize()
+                    .chain_err(|| format_err!("Target path {:?} is not a valid file", target))?;
+                Some(path.into_os_string()
+                    .into_string()
+                    .map_err(|_| format_err!("Target path is not valid unicode"))?)
+            }
+            other => other.clone(),
+        };
+        if let Some(ref s) = requested_target {
             if s.trim().is_empty() {
                 bail!("target was empty")
             }
@@ -90,6 +101,9 @@ impl BuildConfig {
         let cfg_target = config.get_string("build.target")?.map(|s| s.val);
         let target = requested_target.clone().or(cfg_target);
 
+        if jobs == Some(0) {
+            bail!("jobs must be at least 1")
+        }
         if jobs.is_some() && config.jobserver_from_env().is_some() {
             config.shell().warn(
                 "a `-j` argument was passed to Cargo but Cargo is \
index 0abad64f07f1b0b245dd41958065c62f9b8b5efa..ec031b526aeeac9ccd4e223162ebf1064ba5598c 100644 (file)
@@ -23,7 +23,7 @@
 //!
 
 use std::collections::HashSet;
-use std::path::{Path, PathBuf};
+use std::path::PathBuf;
 use std::sync::Arc;
 
 use core::compiler::{BuildConfig, BuildContext, Compilation, Context, DefaultExecutor, Executor};
@@ -34,7 +34,7 @@ use core::{Package, Source, Target};
 use core::{PackageId, PackageIdSpec, TargetKind, Workspace};
 use ops;
 use util::config::Config;
-use util::{lev_distance, profile, CargoResult, CargoResultExt};
+use util::{lev_distance, profile, CargoResult};
 
 /// Contains information about how a package should be compiled.
 #[derive(Debug)]
@@ -229,22 +229,6 @@ pub fn compile_ws<'a>(
         ref export_dir,
     } = *options;
 
-    let target = match target {
-        &Some(ref target) if target.ends_with(".json") => {
-            let path = Path::new(target)
-                .canonicalize()
-                .chain_err(|| format_err!("Target path {:?} is not a valid file", target))?;
-            Some(path.into_os_string()
-                .into_string()
-                .map_err(|_| format_err!("Target path is not valid unicode"))?)
-        }
-        other => other.clone(),
-    };
-
-    if jobs == Some(0) {
-        bail!("jobs must be at least 1")
-    }
-
     let rustc_info_cache = ws.target_dir()
         .join(".rustc_info.json")
         .into_path_unlocked();